package tests;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

import org.springframework.util.Assert;
import org.junit.jupiter.api.Assertions;

public class CreatePrizePage extends Utils {

    private static String url = "http://82.156.185.199:8080/admin.html";

    public CreatePrizePage() {
        super(url);
    }

    public void testPage(){
        driver.findElement(By.cssSelector("body > div.cont-box > div.sidebar > ul > li:nth-child(2) > ul > li:nth-child(2) > a")).click();
        driver.switchTo().frame(0);
        // "开始创建奖品"
        driver.findElement(By.cssSelector("body > div > h2"));
        // "奖品名称"
        driver.findElement(By.cssSelector("#prizeRows > div > div:nth-child(1) > label"));
        // "奖品名称" 输入框
        driver.findElement(By.cssSelector("#prizeName"));
        // "奖品图片"
        driver.findElement(By.cssSelector("#prizeRows > div > div:nth-child(2) > label"));
        // "上传图片框"
        driver.findElement(By.cssSelector("#prizeImageUrl"));
        // "奖品价格"
        driver.findElement(By.cssSelector("#prizeRows > div > div:nth-child(3) > label"));
        // "奖品价格" 输入框
        driver.findElement(By.cssSelector("#price"));
        // "奖品描述"
        driver.findElement(By.cssSelector("#prizeRows > div > div:nth-child(4) > label"));
        // "奖品描述" 输入框
        driver.findElement(By.cssSelector("#description"));
        // "创建奖品" 按钮
        driver.findElement(By.cssSelector("body > div > button"));
    }

    /**
     * 创建奖品
     */
    public void createPrize() throws InterruptedException {

        // 创建奖品成功
        driver.findElement(By.cssSelector("#prizeName")).sendKeys("仔仔十七号");
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id=\"prizeImageUrl\"]")).sendKeys("C:\\Users\\fangh\\Pictures\\素材87.jpg");
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#price")).sendKeys("20");
        driver.findElement(By.cssSelector("#description")).sendKeys("仔仔十七号");
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("body > div > button")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String expectText = "奖品创建成功！";
        String actualText = alert.getText();
        System.out.println("弹窗文本：" + actualText);
        Assertions.assertEquals(expectText, actualText);
        alert.accept();

        // 创建奖品失败
        // 什么都不填写
        // 刷新页面
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("body > div.cont-box > div.sidebar > ul > li:nth-child(2) > ul > li:nth-child(2) > a")).click();
        driver.switchTo().frame(0);
        driver.findElement(By.cssSelector("body > div > button")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert1 = driver.switchTo().alert();
        String alterText = alert1.getText();
        Assertions.assertTrue(alterText.contains("创建奖品失败"), "弹窗文本不包含'创建奖品失败'");
        alert1.accept();

    }
}
